package com.xiaoq.shop.endpoint;

import com.xiaoq.BaseEndpoint2B;
import com.xiaoq.coms.GenericDataResponse;
import com.xiaoq.coms.GenericResponse;
import com.xiaoq.coms.util.ConstantUtil;
import com.xiaoq.coms.util.IdGenerator;
import com.xiaoq.shop.entity.ShopPayTypeEntity;
import com.xiaoq.shop.service.IShopPayTypeService;
import com.xiaoq.shop.vo.ShopPayTypeVo;
import io.swagger.annotations.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenjh on 2017/11/30.
 */
@RestController
@RequestMapping(value = "/api/v1/shops")
@Api(tags={"商铺的支付方式Api"})
public class ShopPayTypeEndpoint2B extends BaseEndpoint2B{

    private static final class ShopPayTypeResponse extends GenericDataResponse<ShopPayTypeVo> {};

    @Autowired
    private IShopPayTypeService shopPayTypeService;

    @Autowired
    private IdGenerator idGenerator;

    @ApiOperation(value="支付方式列表", notes="支付方式列表")
    @ApiResponses({
            @ApiResponse(code = 200, response = ShopPayTypeResponse.class, message = "支付方式列表")
    })
    @GetMapping(value="/payTypes", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<?> payTypes(HttpServletRequest request) {

        String shopId = getShopId();
        if(StringUtils.isBlank(shopId)){
            return GenericResponse.ng("根据当前用户取shopId失败");
        }

        ShopPayTypeEntity shopPayTypeEntity = shopPayTypeService.getByShopId(shopId);

        if(shopPayTypeEntity == null){
            ShopPayTypeEntity shopPayType = new ShopPayTypeEntity();
            shopPayType.setShopType("1");
            shopPayType.setId(idGenerator.nextId());
            shopPayType.setShopId(shopId);
            shopPayType.setPayMethods(new ArrayList<>());
            shopPayType.setPayTest(false);
            shopPayTypeService.save(shopPayType);
            shopPayTypeEntity = shopPayType;
        }

        List<String> payMethods = shopPayTypeEntity.getPayMethods();
        if(payMethods == null){
            payMethods = new ArrayList<>();
        }
        ShopPayTypeVo shopPayTypeVo = new ShopPayTypeVo();
        shopPayTypeVo.setPayMethods(payMethods);

        return ShopPayTypeResponse.okWithData(shopPayTypeVo);
    }

    @ApiOperation(value="修改支付方式", notes="修改支付方式")
    @ApiResponses({
            @ApiResponse(code = 200, response = GenericResponse.class, message = "修改支付方式")
    })
    @PutMapping(value="/payTypes", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<?> addPayTypes(HttpServletRequest request,
                                         @ApiParam(value="shopPayTypeVo", required=true) @RequestBody ShopPayTypeVo shopPayTypeVo) {

        if(shopPayTypeVo == null){
            return GenericResponse.ng("RequestBody 不能为空");
        }

        String shopId = getShopId();

        if(StringUtils.isEmpty(shopId)){
            return GenericResponse.ng("未获取到登录用户所属店ID");
        }

        ShopPayTypeEntity shopPayTypeEntity = new ShopPayTypeEntity();
        shopPayTypeEntity.setShopId(shopId);
        shopPayTypeEntity.setPayMethods(shopPayTypeVo.getPayMethods());
        boolean successFlag = shopPayTypeService.updateByShopId(shopPayTypeEntity);
        if(successFlag){
            return GenericResponse.ok();
        }

        return GenericResponse.ng("支付方式修改失败");
    }

}